package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.RConst;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.mapper.DriverSettingMapper;
import cn.itsource.mapper.DriverSummaryMapper;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.pojo.domain.DriverSetting;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.pojo.vo.DriverSummaryVo;
import cn.itsource.service.IDriverAuthMaterialService;
import cn.itsource.utils.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author zong
 * @since 2024-08-31
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverSummaryMapper driverSummaryMapper;
    @Autowired
    private DriverSettingMapper driverSettingMapper;
    @Override
    public boolean save(DriverAuthMaterial entity) {
        int count = super.count(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId,entity.getDriverId())
                .ne(DriverAuthMaterial::getRealAuthStatus, RConst.Num.INT_2));
        if(count == RConst.Num.INT_0){
            //设置id
            entity.setId(IdUtil.getId());
            //设置手持身份证
            entity.setIdcardHolding(entity.getIdcardFront());
            //设置驾驶证的反面和手持
            entity.setDrcardBack(entity.getDrcardFront());
            entity.setDrcardHolding(entity.getDrcardFront());

            entity.setRealAuthStatus(RConst.Num.INT_0);
            entity.setCreateTime(new Date());

            //在司机提交审核数据时  初始化司机结算表
            DriverSummary driverSummary = new DriverSummary();
            driverSummary.setId(entity.getDriverId());//用司机的id
            driverSummary.setCreateTime(new Date());
            driverSummaryMapper.insert(driverSummary);
            // 在司机提交审核数据时  初始化司机配置
            DriverSetting setting = new DriverSetting();
            setting.setListenService(false);//不自动听单
            setting.setOrderDistance(RConst.Num.INT_50);//默认值
            setting.setRangeDistance(RConst.Num.INT_5);
            setting.setId(entity.getDriverId());//用司机的id
            driverSettingMapper.insert(setting);
            return super.save(entity);
        }
        return false;
    }

    /**
     * 获取当前登录人的审核状态
     * @return
     */
    @Override
    public DriverSummaryVo driverState() {
        //获取到当前登录人的id
        long driveId = StpUtil.getLoginIdAsLong();
        //在根据id查询审核状态
        DriverAuthMaterial one = this.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getRealAuthStatus, RConst.Num.BASE_LONG_1)
                .eq(DriverAuthMaterial::getDriverId, driveId));
        //返回状态
        DriverSummaryVo driverSummaryVo = new DriverSummaryVo();
        //返回司机的summary
        if(one!=null){
            driverSummaryVo.setRealAuthSuccess(true);
            DriverSummary driverSummary = driverSummaryMapper.selectById(driveId);
            driverSummaryVo.setTodayIncome(driverSummary.getTodayIncome());
            driverSummaryVo.setDriveDuration(driverSummary.getDriveDuration());
            driverSummaryVo.setTodayTradeOrders(driverSummary.getTodayTradeOrders());
            return driverSummaryVo;
        }else {
            driverSummaryVo.setRealAuthSuccess(false);
            return driverSummaryVo;
        }
    }
}
